<include file="Public/head"/>
<include file="Public/common_header"/>
<script src="http://img1.isou365.cn/paimai/yunying/js/global.js"></script>
<div class="mian">
    <include file="Public/sidebar" menu="xtgl.cxkgl"/>
    <div class="mianR">
        <h3 class="yonghu">品牌管理</h3>

        <div class="yuyue">
            <ul>
                <li class="yuCurrent">主品牌管理</li>
                <li class="">品牌管理</li>
                <li class="">车系管理</li>
                <li class="">车型管理</li>
            </ul>
        </div>
        <input type="hidden" name="curr_char" value="{$curr_char}">
        <input type="hidden" name="curr_tag" value="mbrand">
        <!--//----------搜索条件--------->
        <div class="letter_btn clearfix">
            <if condition="$can_manage">
                <a class="btn" href="#" id="masterBrand_btn">新增主品牌</a>
                <a class="btn" href="#" id="brand_btn">新增品牌</a>
                <a class="btn" href="#" id="car_btn">新增车系</a>
                <a class="btn" href="/CarModel/model_add" target="_blank" id="models_btn">新增车型</a>
            </if>

            品牌首字母：
            <volist name="first" id="v">
                <span value="{$v}" onclick="getMaster(this);" class="fchar <php>if($v == $curr_char) echo 'currc';</php>">{$v}</span>
            </volist>

            <div class="brandSelect">
                主品牌：<select id="masterSel" onchange="if($('input[name=curr_tag]').val()=='series' || $('input[name=curr_tag]').val()=='model')brand_select();else brand_list(1);">
                <php>foreach($masterSel AS $key => $val):</php>
                    <option value="{$val.mbrand_id}">{$val.name}</option>
                <php>endforeach;</php>
                </select>
                <span>品牌： <select id="brandSel" onchange="if($('input[name=curr_tag]').val()=='series')series_list(1);else series_select()"></select></span>
                <span>车系： <select id="seriesSel" onchange="model_list(1)"></select></span>
            </div>

        </div>

        <div class="listBox">
            <!--//----------主品牌列表--------->
            <div class="yuList">
                <div id="master_list"></div>
            </div>
            <!--//----------品牌列表--------->
            <div class="yuList" style="display:none">
                <div id="brand_list"></div>
            </div>
            <!--//----------车系列表--------->
            <div class="yuList" style="display:none">
                <div id="series_list"></div>
            </div>
            <!--//----------车型列表--------->
            <div class="yuList" style="display:none">
                <div id="model_list"></div>
            </div>

            <!--//----------新增主品牌--------->
            <div class="master_brand_alert">
            <h3>新增/编辑主品牌</h3>
            <ul>
                <li class="clearfix"><span>主品牌首字母</span>
                    <select name="first_char_master">
                    <option value="">请选择</option>
                        <volist name="first" id="v">
                        <option value="{$v}">{$v}</option>
                        </volist>
                    </select>
                </li>
                <li class="clearfix"><span>主品牌国家</span>
                    <select name="country_master">
                        <php>
                            foreach(C('CAR_MODEL_COUNTRY') as $country){
                                echo '<option value="'.$country.'">'.$country.'</option>';
                            }
                        </php>
                    </select>
                </li>
                <li class="clearfix">
                    <span>主品牌名称</span>
                    <input type="text" class="text input-large" name="name_master" value="">
                    <input type="hidden" name="old_name_master" value="">
                </li>
                <li class="clearfix">
                    <span>主品牌全拼</span>
                    <input type="text" class="text input-large" name="spell_master" value="">
                </li>
<!--
                <li class="clearfix">
                    <span>是否启用</span>
                    <select id="status_master" >
                        <volist name="status" id="v">
                        <option value="{$key}" >{$v}</option>
                        </volist>
                    </select>
                </li>
-->
            </ul>
            <div class="form-item" style="height:50px;width:420px;">
                <button class="btn submit-btn ajax-post btn_master" type="submit" style="line-height:20px">确 定</button>
                <button class="btn btn-return" style="line-height:18px">取 消</button>
            </div>
            </div>

            <!--//----------新增品牌--------->
            <div class="master_brand_alert">
                <h3>新增/编辑品牌</h3>
                <ul>
                    <li class="clearfix">
                        <span>主品牌首字母</span>
                        <select onchange="getBrand('master_id_brand');" id="first_char_brand">
                        <option value="">请选择</option>
                        <volist name="first" id="v">
                        <option value="{$v}">{$v}</option>
                        </volist>
                        </select>
                    </li>
                    <li class="clearfix">
                        <span>选择主品牌</span>
                        <select id="master_id_brand" >
                            <option value="">请选择</option>
                        </select>
                    </li>
                    <li class="clearfix">
                        <span>选择厂商</span>
                        <select id="manufacturer_id_brand" >
                            <option value="" >请选择</option>
                            <volist name="manufacturer" id="v">
                            <option value="{$v.manufacturer_id}">{$v.spell} {$v.short_name}</option>
                            </volist>
                        </select>
                    </li>

                    <li class="clearfix">
                        <span>品牌名称</span>
                        <input type="text" class="text input-large" name="name_brand" value="">
                        <input type="hidden" name="old_name_brand" value="">
                    </li>
                    <li class="clearfix">
                        <span>品牌全拼</span>
                        <input type="text" class="text input-large" name="spell_brand" value="">
                    </li>
<!--
                    <li class="clearfix">
                        <span>是否启用</span>
                        <select id="status_brand" >
                            <volist name="status" id="v">
                            <option value="{$key}">{$v}</option>
                            </volist>
                        </select>
                    </li>
-->
                </ul>
                <div class="form-item" style="height:50px;width:420px;">
                    <button class="btn submit-btn ajax-post btn_brand" type="submit" style="line-height:20px">确 定</button>
                    <button class="btn btn-return" style="line-height:18px">取 消</button>
                </div>
            </div>

            <!--//----------新增车系--------->
            <div class="master_brand_alert">
                <h3>新增/编辑车系</h3>
                <ul>
                    <li class="clearfix">
                        <span>主品牌首字母</span>
                        <select onchange="getBrand('master_id_series');" id="first_char_series" >
                            <option value="">请选择</option>
                            <volist name="first" id="v">
                            <option value="{$v}">{$v}</option>
                            </volist>
                        </select>
                    </li>
                    <li class="clearfix">
                        <span>选择主品牌</span>
                        <select onchange="getBrand('brand_id_series','brand',this.value);" id="master_id_series"></select>
                    </li>

                    <li class="clearfix">
                        <span>选择品牌</span>
                        <select id="brand_id_series" ></select>
                    </li>
                    <li class="clearfix"><span>销售状态</span>
                        <select id="sale_status">
                            <php>
                                foreach(C('CAR_MODEL_SALE_STATUS') as $k=>$v){
                                    echo '<option value="'.$k.'">'.$v.'</option>';
                                }
                            </php>
                        </select>
                    </li>
                    <li class="clearfix"><span>车系类型</span>
                        <select id="type">
                            <php>
                                foreach(C('CAR_TYPE') as $k=>$v){
                                    echo '<option value="'.$k.'">'.$v.'</option>';
                                }
                            </php>
                        </select>
                    </li>

                    <li class="clearfix">
                        <span>车系名称</span>
                        <input type="text" class="text input-large" name="name_series" value="">
                        <input type="hidden" name="old_name_series" value="">
                    </li>
                    <li class="clearfix">
                        <span>车系全拼</span>
                        <input type="text" class="text input-large" name="spell" value="">
                    </li>
                    <li class="clearfix">
                        <span>车系描述</span>
                        <input type="text" class="text input-large" name="desc" value="">
                    </li>
<!--
                    <li class="clearfix">
                        <span>是否启用</span>
                        <select id="status" >
                            <volist name="status" id="v">
                            <option value="{$key}">{$v}</option>
                            </volist>
                        </select>
                    </li>
-->
                </ul>
                <div class="form-item" style="height:50px;width:420px;">
                    <button class="btn submit-btn ajax-post btn_series" type="submit" style="line-height:20px">确 定</button>
                    <button class="btn btn-return" style="line-height:18px">取 消</button>
                </div>
            </div>

        </div>
    </div>

</div>
<div class="clear"></div>
<div class="yunBei"></div>
<script>
    master_list(1);
    function getMaster(e) {
        $(".currc").removeClass("currc");
        $(e).addClass("currc");
        var curr_char = $(e).text().trim();
        $('input[name=curr_char]').val(curr_char)
        var paramer = {
            "type": 'mbrand',
            "char":curr_char,
            "json":1
        };

        var curr_tag = $('input[name=curr_tag]').val();
        switch(curr_tag){
            case "mbrand":master_list(1);break;
            case "brand":
            case "series":
            case "model":mbrand_select();break;
        }

    }

    //------------------------------主品牌管理-------------------
    function master_list(p) {
        $('#masterSel').hide();
        $('#brandSel').hide();
        $('#seriesSel').hide();
        var curr_char =  $('input[name=curr_char]').val();
        var paramer = {
            "p": p,
            "type": 'mbrand',
            "char":curr_char
        };
        $.get("/CarModel/get_list/",paramer, function (data) {
            $("#master_list").html(data);
        }, "html");
    }
    //添加主品牌
    $('.btn_master').on("click",function(){
        var first_char_master = $('select[name=first_char_master]').val();
        var country_master = $('select[name=country_master]').val();
        var name_master = $('input[name=name_master]').val();
        var old_name_master = $('input[name=old_name_master]').val();
        var spell_master = $('input[name=spell_master]').val();
        var status_master = $('#status_master').val();
        var mbrand_id = $('input[name=mbrand_id]').val();
        if(first_char_master==''){
           shibai_alert('主品牌首字母不能为空');return;
        }
        if(country_master==''){
           shibai_alert('主品牌国家不能为空');return;
        }
        if(name_master==''){
           shibai_alert('主品牌名称不能为空');return;
        }
        if(spell_master==''){
           shibai_alert('主品牌全拼不能为空');return;
        }
        var paramer = {
            "first_char_master": first_char_master,
            "country_master":country_master,
            "name_master": name_master,
            "old_name_master": old_name_master,
            "spell_master":spell_master,
            "status_master":status_master,
            "mbrand_id":mbrand_id
        };

        $.post("/CarModel/master_add/",paramer, function (data) {
            if(data.code=='200'){
                btn_alert(data.msg,2,function(){$('.btn-return').click()});
            }else{
                shibai_alert(data.msg)
            }
        }, "json");

    });
    //------------------------------品牌管理-------------------
    //表单用生成主品牌下拉菜单
    function getBrand(obj,table,mbrand_id){
        var char_obj = '';
        var extra_url = '';
        var type = '';
        var title = '';

        switch(obj){
            case 'master_id_brand':
                char_obj = 'first_char_brand';
                break;
            case 'master_id_series':
                char_obj = 'first_char_series';
                break;
            case 'brand_id_series':
                char_obj = 'first_char_series';
                break;
        }
        var curr_char = $('#'+char_obj).val();
        if(typeof(table)=='undefined'){
            type = 'mbrand';title = '请选择主品牌';
        }else{
            type = 'brand';title = '请选择品牌';
            extra_url += 'mbrand_id/'+mbrand_id+'/';
        }
        var paramer = {
            "type": type,
            "char":curr_char,
            "json":1,
            "rnd":Math.random()
        };
        $("#"+obj+" option").remove();

        $.get("/CarModel/get_list/"+extra_url,paramer, function (data) {
            var option = ("<option value='0' >"+title+"</option>");
            if(data.data[0] !== null && data.data[0].length>0){
                if(extra_url !=''){ //品牌
                    for(var i=0;i<data.data[0].length;i++){
                        option += ("<option value='"+data.data[0][i].brand_id+"' >"+data.data[0][i].name+"</option>")
                    }
                }else{ //主品牌
                    for(var i=0;i<data.data[0].length;i++){
                        option += ("<option value='"+data.data[0][i].mbrand_id+"' >"+data.data[0][i].name+"</option>")
                    }
                }
            }else{
                option += ("<option value='0' >无</option>")
            }
            $("#"+obj).append(option);
        }, "json");
    }
    // 品牌列表
    function brand_list(p){
        $('#masterSel').show();
        $('#brandSel').hide();
        $('#seriesSel').hide();

        var mbrand_id = $("#masterSel").val(); //选中的主品牌
        var curr_char = $('input[name=curr_char]').val(); //选中的首字母
        var paramer = {
            "p": p,
            "type": 'brand',
            "char":curr_char,
            "mbrand_id":mbrand_id
        };
        $.get("/CarModel/get_list/",paramer, function (data) {
            $("#brand_list").html(data);
        }, "html");
        //
    }
    //添加品牌
    $('.btn_brand').on("click",function(){

        var first_char_brand = $('#first_char_brand').val();
        var mbrand_id = $('#master_id_brand').val();
        var manufacturer_id = $('#manufacturer_id_brand').val();
        var name_brand = $('input[name=name_brand]').val();
        var old_name_brand = $('input[name=old_name_brand]').val();
        var spell_brand = $('input[name=spell_brand]').val();
        var status_brand = $('#status_brand').val();

        var brand_id = $('input[name=brand_id_brand]').val();

        if(first_char_brand==''){
           shibai_alert('请选择主品牌首字母');return;
        }
        if(mbrand_id<1){
           shibai_alert('请选择主品牌');return;
        }
        if(manufacturer_id<1){
           shibai_alert('请选择厂商');return;
        }
        if(name_brand==''){
           shibai_alert('请填写品牌名称');return;
        }
        if(spell_brand==''){
           shibai_alert('请填写品牌全拼');return;
        }
        var paramer = {
            "first_char_brand":first_char_brand,
            "mbrand_id":mbrand_id,
            "manufacturer_id":manufacturer_id,
            "name_brand": name_brand,
            "old_name_brand": old_name_brand,
            "spell_brand":spell_brand,
            "status_brand":status_brand,
            "brand_id":brand_id
        };

        $.post("/CarModel/brand_add/",paramer, function (data) {
            if(data.code=='200'){
                btn_alert(data.msg,2,function(){
                    $('.btn-return').click();

                });
            }else{
                shibai_alert(data.msg)
            }
        }, "json");

    });
    //-----------------------------车系管理--------------------------
    //车系列表
    function series_list(p){
        $('#masterSel').show();
        $('#brandSel').show();
        $('#seriesSel').hide();
        var curr_char = $('input[name=curr_char]').val(); //选中的首字母
        var brand_id = $("#brandSel").val(); //选中的主品牌
        var paramer = {
            "p": p,
            "type":'series',
            "brand_id":brand_id
        };
        $.get("/CarModel/get_list/",paramer, function (data) {
            $("#series_list").html(data);
        }, "html");
        //
    }
    //添加车系
    $('.btn_series').on("click",function(){

        var brand_id = $('#brand_id_series').val();
        var sale_status = $('#sale_status').val();
        var type = $('#type').val();

        var name = $('input[name=name_series]').val();
        var old_name = $('input[name=old_name_series]').val();
        var spell = $('input[name=spell]').val();
        var desc = $('input[name=desc]').val();
        var status = $('#status').val();

        var series_id = $('input[name=series_id]').val();

        if(brand_id<1){
           shibai_alert('请选择品牌');return;
        }
        if(sale_status==''){
           shibai_alert('请选择销售状态');return;
        }

        if(type<1){
           shibai_alert('请选择车系类型');return;
        }

        if(name==''){
           shibai_alert('请填写车系名称');return;
        }
        if(spell==''){
           shibai_alert('请填写车系全拼');return;
        }
        var paramer = {
            "brand_id":brand_id,
            "sale_status":sale_status,
            "type":type,

            "name": name,
            "old_name": old_name,
            "spell":spell,
            "desc":desc,
            "status":status,

            "series_id":series_id
        };

        $.post("/CarModel/series_add/",paramer, function (data) {
            if(data.code=='200'){
                btn_alert(data.msg,2,function(){$('.btn-return').click()});
            }else{
                shibai_alert(data.msg)
            }
        }, "json");

    });
    //-----------------------------车型管理--------------------------
    //车型列表
    function model_list(p){
        $('#masterSel').show();
        $('#brandSel').show();
        $('#seriesSel').show();
        var series_id = $("#seriesSel").val(); //选中的主品牌
        var paramer = {
            "p": p,
            "type":'model',
            "series_id":series_id
        };
        $.get("/CarModel/get_list/",paramer, function (data) {
            $("#model_list").html(data);
        }, "html");
    }
    //-----------------------------下拉菜单-------------------------------

    //初始化主品牌下拉菜单
    function mbrand_select(){
        var curr_char = $('input[name=curr_char]').val();
        var mbrand_id = $('#masterSel').val();
        var paramer = {
            "type": 'mbrand',
            "char":curr_char,
            "json":1,
            "rnd":Math.random()
        };

        $("#masterSel option").remove();
        $.get("/CarModel/get_list/",paramer, function (data) {
            var option = ("<option value='0' >请选择主品牌</option>");
            if(data.data[0] !== null && data.data[0].length>0){
                for(var i=0;i<data.data[0].length;i++){
                    option += ("<option value='"+data.data[0][i].mbrand_id+"' "+(mbrand_id==data.data[0][i].mbrand_id ? 'selected' :'')+" >"+data.data[0][i].name+"</option>")
                }
            }else{
                option += ("<option value='0' >无</option>")
            }
            $("#masterSel").append(option);
        }, "json");
        var curr_tag = $('input[name=curr_tag]').val();
        switch(curr_tag){
            case "brand":brand_list(1);break;
            case "series":
            case "model": brand_select();break;
        }
    }

    //初始化品牌下拉菜单
    function brand_select(){
        var curr_char = $('input[name=curr_char]').val();
        var mbrand_id = $("#masterSel").val();
        var brand_id = $("#brandSel").val();
        var paramer = {
            "type": 'brand',
            "char":curr_char,
            "mbrand_id":mbrand_id,
            "rnd":Math.random()
        };
        $.get("/CarModel/get_list/json/1/",paramer, function (data) {
            $("#brandSel option").remove();
            var option = ("<option value='0' >请选择品牌</option>")
            if(data.data[0] !== null && data.data[0].length>0){
                for(var i=0;i<data.data[0].length;i++){
                    option += ("<option value='"+data.data[0][i].brand_id+"' "+(brand_id==data.data[0][i].brand_id ? 'selected' :'')+">"+data.data[0][i].name+"</option>")
                }
            }else{
                option += ("<option value='0'>无</option>")
            }
            $("#brandSel").append(option)
        }, "json");

        var curr_tag = $('input[name=curr_tag]').val();
        switch(curr_tag){
            case "series":series_list(1);break;
            case "model": series_select(); break;
        }
    }
    //初始化车系下拉菜单
    function series_select(){
        var brand_id  = $('#brandSel').val();
        var series_id  = $('#seriesSel').val();
        var paramer = {
            "p": 1,
            "type": 'series',
            "brand_id":brand_id
        };

        $.get("/CarModel/get_list/json/1/",paramer, function (data) {
            $("#seriesSel option").remove();
            var option = ("<option value='0' >请选择车系</option>")
            if(data.data[0] !== null && data.data[0].length>0){
                for(var i=0;i<data.data[0].length;i++){
                    option += ("<option value='"+data.data[0][i].series_id+"' "+(series_id==data.data[0][i].series_id ? 'selected' :'')+">"+data.data[0][i].name+"</option>")
                }
            }else{
                option += ("<option value='0'>无</option>")
            }
            $("#seriesSel").append(option);
        }, "json");
        //
        var curr_tag = $('input[name=curr_tag]').val();
        switch(curr_tag){
            case "model":model_list(1);break;
        }
    }
    //-------------------------------------------------
    $(function () {
        $("select").css("color", "#000");
        $("select").find("option").css("color", "#000");
        $("select").each(function () {
            $(this).change(function () {
                var selectIndex = $(this).get(0).selectedIndex;
                if (selectIndex == 0) {
                    $(this).css("color", "#000");
                }
            })
        });
    })

    $(document).ready(function (e) {
        var brand_height = $(".master_brand_alert").outerHeight();
        $(".master_brand_alert").css("marginTop", (-brand_height) / 2);
        var brand_height2 = $(".master_brand_alert").eq(2).outerHeight();
        $(".master_brand_alert").eq(2).css("marginTop", -brand_height2 / 2);
        $(".yunBei").unbind();

        $('.brandSelect').hide();
		$(".yuyue ul li").eq(0).click(function(){
            $('input[name=curr_tag]').val('mbrand');
			$("#brand_btn,#car_btn,#models_btn").hide();
			$("#masterBrand_btn").show();
			$('.brandSelect').hide();
            master_list(1);
		})
		$(".yuyue ul li").eq(1).click(function(){
            $('input[name=curr_tag]').val('brand');
			$("#masterBrand_btn,#car_btn,#models_btn").hide();
			$("#brand_btn").show();
			$('.brandSelect').show();
			$('.brandSelect span').hide();
            $('#masterSel').val(0);
            mbrand_select();
		})
		$(".yuyue ul li").eq(2).click(function(){
            $('input[name=curr_tag]').val('series');
			$("#masterBrand_btn,#brand_btn,#models_btn").hide();
			$("#car_btn").show();
			$('.brandSelect').show();
			$('.brandSelect span').hide();
			$('.brandSelect span').eq(0).show();
            $('#masterSel').val(0);$('#brandSel').val(0);
            mbrand_select();
		})
		$(".yuyue ul li").eq(3).click(function(){
            $('input[name=curr_tag]').val('model');
			$("#masterBrand_btn,#brand_btn,#car_btn").hide();
			$("#models_btn").show();
			$('.brandSelect').show();
			$('.brandSelect span').show();
            $('#masterSel').val(0);$('#brandSel').val(0);$('#seriesSel').val(0);
            mbrand_select();
		})


        $(".letter_btn .btn").eq(0).click(function () {
            $(".master_brand_alert").eq(0).fadeIn();
            $(".yunBei").show();
        })
        $(".form-item .btn-return").click(function () {
            $(".master_brand_alert").eq(0).fadeOut();
            $(".yunBei").hide();
        })

        $(".letter_btn .btn").eq(1).click(function () {
            $(".master_brand_alert").eq(1).fadeIn();
            $(".yunBei").show();
        })
        $(".form-item .btn-return").click(function () {
            $(".master_brand_alert").eq(1).fadeOut();
            $(".yunBei").hide();
        })
        $(".letter_btn .btn").eq(2).click(function () {
            $(".master_brand_alert").eq(2).fadeIn();
            $(".yunBei").show();
        })
        $(".form-item .btn-return").click(function () {
            $(".master_brand_alert").eq(2).fadeOut();
            $(".yunBei").hide();
        })

        $(".data-table").eq(0).find(".editBtn").click(function () {
            $(".master_brand_alert").eq(0).fadeIn();
            $(".yunBei").show();
        })
        $(".data-table").eq(1).find(".editBtn").click(function () {
            $(".master_brand_alert").eq(1).fadeIn();
            $(".yunBei").show();
        })
        $(".data-table").eq(2).find(".editBtn").click(function () {
            $(".master_brand_alert").eq(2).fadeIn();
            $(".yunBei").show();
        })

    });
</script>

<include file="Public/foot"/>

